<?php
/**
 * Project: Vietsam
 * Package Controller
 * Class: Login
 * Author: Khoi Pham		email: pmkhoi@gmail.com
 * Created on Dec 18, 2008
 * Updated on Dec 30, 2008
 *
 * */

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Login extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('tab_model', 'tab', true);

        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->template->set_master_template('login');
        $this->template->add_region('error_message');
    }

    function Login() {
        parent::__construct();
        $this->load->model('tab_model', 'tab', true);
        //$this->load->model('user_model', 'user', true);
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->template->set_master_template('login');
        $this->template->add_region('error_message');
    }

    function index() {

        if (isset($_POST['btLogin'])) {

            // $code = $this->input->post('txtCode');
            $email = trim($this->input->post('txtEmail', true));
            $password = trim($this->input->post('txtPassword'));

            $this->form_validation->set_rules('txtEmail', 'Email', 'required|valid_emssail');
            $this->form_validation->set_rules('txtPassword', 'Password', 'required');


            if ($this->form_validation->run() == FALSE) {
                $this->template->write('error_message', validation_errors());
            } else {
                $this->load->model('user_model', 'user', true);

                $user = $this->user->getByEmailPassword($email, $password);


                if ($user) {
                    //$_SESSION['user'] = $user;
                    $this->session->set_userdata('user', $user);
                    //redirect('admin/home');
                    redirect('admin/ncategory');
                    exit();
                } else {
                    $this->template->write('error_message', lang('email_password_not_match'));
                }
            }
        }

        $this->template->render();
    }

    function signin() { //echo md5('159753123');
        $code = $this->input->post('txtCode');
        $email = trim($this->input->post('txtEmail', true));
        $password = trim($this->input->post('txtPassword'));

        $error = false;
        $error_message = '';

        if ($email == '') {
            $error = true;
            $error_message .= $this->lang->line('email_is_required') . '<br>';
        } else {
            $this->load->helper('email');
            if (!valid_email($email)) {
                $error = true;
                $error_message .= $this->lang->line('email_invalid') . '<br>';
            }
        }

        if ($password == '') {
            $error = true;
            $error_message .= $this->lang->line('password_is_required') . '<br>';
        }

        if ($error) {
            $this->setTemplate('admin/login/login_form.tpl');
            echo $this->main = eval($this->loadTemplate('admin/login/login_form.tpl'));
            //$this->execute();
        } else {

            $this->load->model('user_model', 'user', true);
            $user = $this->user->getByEmailPassword($email, $password);


            if ($user) {
                /* $user = array(
                  'id_employee' => $employee->id_employee,
                  'id_profile' => $employee->id_profile,
                  'firstname' => $employee->firstname,
                  'lastname' => $employee->lastname
                  );
                 */
                //$this->session->set_userdata('user', $user);
                $_SESSION['user'] = $user;
                redirect('admin/home');
            } else {
                $error_message = $this->lang->line('email_password_not_match');
                $this->setTemplate('admin/login/login_form.tpl');
                echo $this->main = eval($this->loadTemplate('admin/login/login_form.tpl'));
                //$this->execute();
            }
        }
    }

    function signout() {
        unset($_SESSION['user']);
        redirect('admin/login');
    }

}

?>